package com.km.team.phsystem.service.impl;

import cn.hutool.core.date.DateUtil;
import com.km.team.phsystem.beans.pojo.QueueInfo;
import com.km.team.phsystem.beans.pojo.QueueInfoExample;
import com.km.team.phsystem.beans.pojo.TemporaryAppointment;
import com.km.team.phsystem.beans.pojo.TemporaryAppointmentExample;
import com.km.team.phsystem.common.constants.ConstantsDateInformation;
import com.km.team.phsystem.common.enums.QueueStatusEnum;
import com.km.team.phsystem.common.enums.ReservationTypeEnum;
import com.km.team.phsystem.dao.mybatis.QueueInfoMapper;
import com.km.team.phsystem.dao.mybatis.TemporaryAppointmentMapper;
import com.km.team.phsystem.dao.mybatis.custom.QueueInfoMapperCustom;
import com.km.team.phsystem.dao.mybatis.custom.TemporaryAppointmentMapperCustom;
import com.km.team.phsystem.service.ISysBusiTypeService;
import com.km.team.phsystem.service.TemporaryAppointmentService;
import com.km.team.phsystem.service.impl.base.BaseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：Riven.
 * @description：临时排号窗口
 * @date ：2018/12/5
 */
@Service("temporaryAppointmentService")
public class TemporaryAppointmentServiceImpl extends BaseService<TemporaryAppointmentServiceImpl,
    TemporaryAppointment> implements TemporaryAppointmentService {
    private final TemporaryAppointmentMapper temporaryAppointmentMapper;
    private final TemporaryAppointmentMapperCustom temporaryAppointmentMapperCustom;
    private final QueueInfoMapper queueInfoMapper;
    private final QueueInfoMapperCustom queueInfoMapperCustom;

    @Autowired
    public TemporaryAppointmentServiceImpl(TemporaryAppointmentMapper temporaryAppointmentMapper,
                                           TemporaryAppointmentMapperCustom temporaryAppointmentMapperCustom,
                                           QueueInfoMapper queueInfoMapper,
                                           QueueInfoMapperCustom queueInfoMapperCustom) {
        this.temporaryAppointmentMapper = temporaryAppointmentMapper;
        this.temporaryAppointmentMapperCustom = temporaryAppointmentMapperCustom;
        this.queueInfoMapper = queueInfoMapper;
        this.queueInfoMapperCustom = queueInfoMapperCustom;
    }

    @Override
    public List<TemporaryAppointment> selectForBusinessId(Integer businessId) {
        return getTemporaryAppointmentsForBusinessId(businessId);
    }

    @Override
    public TemporaryAppointment selectOneForBusinessId(Integer businessId) {
        List<TemporaryAppointment> list =
            getTemporaryAppointmentsForBusinessId(businessId);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return new TemporaryAppointment();

    }

    @Override
    public TemporaryAppointment getDateForBusinessId(Integer id) {
        TemporaryAppointmentExample temporaryAppointmentExample = new TemporaryAppointmentExample();
        temporaryAppointmentExample.or().andSystemBusitypeIdEqualTo(id);
        //检查数据是否过期，
        judgingDate();
        return this.temporaryAppointmentMapper.selectByExample(temporaryAppointmentExample).get(0);
    }

    @Override
    public void judgingDate() {
        Date newDate = new Date();
        Date beginOfDay = DateUtil.beginOfDay(newDate);
        List<TemporaryAppointment> temporaryAppointments1 =
            temporaryAppointmentMapper.selectByExample(new TemporaryAppointmentExample());
        if (temporaryAppointments1.size() != 0) {
            if (beginOfDay.after(DateUtil.parse(temporaryAppointments1.get(0).getDate()))) {
                //需要重置
                temporaryAppointments1.forEach(temp -> {
                    temp.setDoAcceptNo(0);
                    temp.setDate(DateUtil.today());
                    this.temporaryAppointmentMapper.updateByPrimaryKeySelective(temp);
                });
            }
            QueueInfoExample queueInfoExample = new QueueInfoExample();
            queueInfoExample.or().andDateLessThan(DateUtil.parse(DateUtil.formatDate(new Date()),
                ConstantsDateInformation.YYYY_MM_DD))
                .andQueueTypeEqualTo(ReservationTypeEnum.RESERVATION_TYPE_TEMPORARY_APPOINTMENT.getType())
            .andQueueStatusEqualTo(QueueStatusEnum.QUEUE_STATUS_SKIP.getType());
            List<QueueInfo> timeOutList = queueInfoMapper.selectByExample(queueInfoExample);
            timeOutList.forEach(queue -> {
                logger.info(queue.getMobile() + " 用户的 " + queue.getBusinessName() + " 临时排号业务已超时！");
                queue.setQueueStatus(QueueStatusEnum.QUEUE_STATUS_TERMINATE.getType());
            });
            if (CollectionUtils.isNotEmpty(timeOutList)) {
                int i = this.queueInfoMapperCustom.batchUpdateForTimeOut(timeOutList);
                logger.info("批量临时预约超时跟新成功! 跟新数量： " + i);
            }
        }

    }

    private List<TemporaryAppointment> getTemporaryAppointmentsForBusinessId(Integer businessId) {
        TemporaryAppointmentExample temporaryAppointmentExample = new TemporaryAppointmentExample();
        temporaryAppointmentExample.or().andSystemBusitypeIdEqualTo(businessId);
        return this.temporaryAppointmentMapper.selectByExample(temporaryAppointmentExample);
    }

    @Override
    public int insert(Integer businessId, Integer ttaNo) {
        TemporaryAppointment temporaryAppointment = new TemporaryAppointment();
        temporaryAppointment.setAcceptNo(ttaNo);
        temporaryAppointment.setDoAcceptNo(0);
        temporaryAppointment.setSystemBusitypeId(businessId);
        temporaryAppointment.setDate(DateUtil.today());
        return this.temporaryAppointmentMapper.insert(temporaryAppointment);
    }

    @Override
    public int update(List<TemporaryAppointment> appointments, Integer ttaNo) {
        appointments.forEach(item -> {
            item.setAcceptNo(ttaNo);
            this.temporaryAppointmentMapper.updateByPrimaryKeySelective(item);
        });
        return appointments.size();
    }

    @Override
    public int addOne(Integer businessId) {
        List<TemporaryAppointment> temporaryAppointmentsForBusinessId =
            this.getTemporaryAppointmentsForBusinessId(businessId);
        temporaryAppointmentsForBusinessId.forEach(item -> {
            Integer doAcceptNo = item.getDoAcceptNo();
            item.setDoAcceptNo(doAcceptNo + 1);
            this.temporaryAppointmentMapper.updateByPrimaryKeySelective(item);
        });
        return temporaryAppointmentsForBusinessId.size();
    }

    @Override
    public int reduceOne(Integer business) {
        List<TemporaryAppointment> temporaryAppointmentsForBusinessId =
            this.getTemporaryAppointmentsForBusinessId(business);
        temporaryAppointmentsForBusinessId.forEach(item -> {
            Integer doAcceptNo = item.getDoAcceptNo();
            item.setDoAcceptNo(doAcceptNo - 1);
            this.temporaryAppointmentMapper.updateByPrimaryKeySelective(item);
        });
        return temporaryAppointmentsForBusinessId.size();
    }

    @Override
    public int deleteForBusinessId(Integer business) {
        TemporaryAppointmentExample temporaryAppointmentExample = new TemporaryAppointmentExample();
        temporaryAppointmentExample.or().andSystemBusitypeIdEqualTo(business);
        return this.temporaryAppointmentMapperCustom.batchDelete(this.temporaryAppointmentMapper.selectByExample(temporaryAppointmentExample));
    }
}
